/*jshint esversion:6*/
const app = getApp();
const _ = require('../../libs/lodash.js');
const X = require('../../libs/xman.js');
const config = require('../../config.js');

Page({
    data: {
        postID: '',
        loading: true,
        item: {},
        showCommentBox: false,
        userInfo: {},
        comments: [],
        isLoadMore: false,
        page: 1,
        noPageLoad: false,
        commentsNum: 0
    },
    getCommentOption(postId = this.data.postId) {
        return ({
            query: '{"belong":"' + postId + '"}',
            order: '-createdAt',
            page: this.data.page
        });
    },
    showCommentBox() {
        this.setData({
            showCommentBox: true
        });
    },
    hideCommentBox() {
        this.setData({
            showCommentBox: false
        });
    },
    postComment(e) {
        let comment = e.detail.value;
        this.setData({
            showCommentBox: false
        });
        // POST评论 -> PUT文章
        X.post('comments', {
                content: comment.content,
                belong: this.data.postId,
                userInfo: this.data.userInfo
            })
            .then((res) => {
                X.get('comments', res.objectId).then((res) => {
                    this.data.comments.unshift(res);
                    this.setData({ comments: this.data.comments });
                });
            })
            .then(() => {
                X.put('goods', this.data.postId, {
                    commentsNum: { "__op": "Increment", "amount": 1 }
                }).then(() => { this.setData({ commentsNum: this.data.commentsNum + 1 }); });
            })
            .catch((err) => {
                console.error(err);
            });
    },
    scrollBottom() {
        if (!this.data.noPageLoad && !this.data.isLoadMore) {

            this.setData({
                isLoadMore: true,
                page: this.data.page + 1
            });
            X.get('comments', this.getCommentOption())
                .then((res) => {
                    this.setData({
                        comments: this.data.comments.concat(res.results),
                        isLoadMore: false,
                    });
                    if (res.results.length < config.maxPerPage) {
                        this.setData({
                            noPageLoad: true
                        });
                    }

                });
        }
    },
    onLoad: function(params) {
        this.setData({
            postId: params.id
        });

        X.wxGetUserInfo().then((userInfo) => {
            this.setData({
                userInfo: userInfo
            });
        });

        X.get('goods', this.data.postId)
            .then((res) => {
                this.setData({
                    loading: false,
                    commentsNum: res.commentsNum,
                    item: res
                });
                return res.objectId;
            })
            .then((id) => {
                return X.get('comments', this.getCommentOption()).then((res) => {
                    this.setData({
                        comments: res.results
                    });
                });
            })
            .catch((error) => {
                console.error(error);
            });


    },

    onReady: function() {
        // 页面渲染完成

    },
    onShow: function() {
        // 页面显示
    },
    onHide: function() {
        // 页面隐藏
    },
    onUnload: function() {
        // 页面关闭
    }
});
